//
//  AddUserSetting.swift
//  YoruNeko
//
//  Created by Kotori on 2021/6/21.
//

import SwiftUI

struct AddUserSetting: View {
    @State var username: String = ""
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        VStack(alignment: .center) {
            
            Text("为配置文件命名")
                .padding()
                .padding(.horizontal)
                .padding(.top)
                .font(.title2)
            
            
            TextField("用户名", text: $username)
                .frame(height: 40)
                .cornerRadius(10)
                .padding(.horizontal)
                .background(RoundedRectangle(cornerRadius: 20).foregroundColor(Color.init(.systemGray6)))
                .padding()
            
            Text("创建个人资料即可将工作历史记录和个人历史记录分开保存，或者为每位家人创建不同的配置文件。")
                .padding()
                .padding(.horizontal)
                .font(.headline)
            
            Button {
                addUser(username: username)
            } label: {
                Text("保存")
            }
            
            Spacer()
            

        }
        .navigationTitle("添加用户配置")
        .navigationBarTitleDisplayMode(.large)
    }
    
    func addUser(username: String) {
        if !username.isEmpty {
            let userMgr = UserPreferenceManager.manager(of: UUID())
            userMgr.setUsername(nickname: username)
            userMgr.commit()
        }
        self.presentationMode.wrappedValue.dismiss()
    }
}

struct AddUserSetting_Previews: PreviewProvider {
    static var previews: some View {
        AddUserSetting()
    }
}
